<?php
/**
 * Created by PhpStorm
 * USER MingTingLiang   QQ 915802070
 * Date 2020-12-17-017   11:10
 */

$dataFromDB = '[{"newsid":"201","pubtime":"2015-05-06","title":"工程师打造超强团队","desc":"我们是人真的"},{"newsid":"202","pubtime":"2015-05-07","title":"工程师打造超强兵器","desc":"我们是人真的"},{"newsid":"203","pubtime":"2015-05-08","title":"工程师打造超强大脑","desc":"我们是人真的"}]';


global $news;
$news = json_decode($dataFromDB, 1);

//var_dump($news);
class  News implements Iterator, ArrayAccess, JsonSerializable
{
    public $data = [];
    public $pos = 0;

    public function __construct()
    {
        global $news;
        $this->data = $news;
    }

    public function current()
    {
        // TODO: Implement current() method.
        $result = (object)$this->data[$this->pos];
        $result->index = $this->pos + 1;
        return $result;
    }

    public function next()
    {
        // TODO: Implement next() method.
        $this->pos++;
    }

    public function key()
    {
        // TODO: Implement key() method.
//        return $this->current()['newsid'];
        return $this->current()->newsid;
    }

    public function valid()
    {
        // TODO: Implement valid() method.
        return isset($this->data[$this->pos]);
    }

    public function rewind()
    {
        // TODO: Implement rewind() method.
        $this->pos = 0;
    }

    public function offsetExists($offset)
    {
        // TODO: Implement offsetExists() method.
        return isset($this->data[$offset]);
    }

    public function offsetGet($offset)
    {
        // TODO: Implement offsetGet() method.
        return $this->data[$offset];
    }

    public function offsetSet($offset, $value)
    {
        // TODO: Implement offsetSet() method.
        $this->data[$offset] = $value;
    }

    public function offsetUnset($offset)
    {
        // TODO: Implement offsetUnset() method.
        unset($this->data[$offset]);
    }

    public function jsonSerialize()
    {
        // TODO: Implement jsonSerialize() method.
        return json_encode($this->data);
    }

    public function __invoke($pos)
    {
        // TODO: Implement __invoke() method.
        return (object)$this->data[$pos];
    }
}

$newsList = new News();
var_dump($newsList[0]['title']);
echo "<hr>";
var_dump($newsList(0)->title);

?>

<?php
echo "<ul>";
$i = 0;
foreach ($newsList as $index => $item) {
    echo "<li><h2>{$item->index},<a href='/newsDetail?id={$item->newsid}'>{$item->title}</a></h2></li>";
    echo "<li><h2>{$index}{$item->desc}</h2></li>";
}
echo "</ul>";

?>
